package com.zoweunion.mechanic.service.base.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.dao.app.OrderDao;
import com.zoweunion.mechanic.dao.app.ToolCarDataMapper;
import com.zoweunion.mechanic.dao.base.KanBanDao;
import com.zoweunion.mechanic.model.things.ThingsData;
import com.zoweunion.mechanic.service.base.KanBanService;
import com.zoweunion.mechanic.util.DateUtil;
import com.zoweunion.mechanic.util.HttpClient;
import com.zoweunion.mechanic.util.Page;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 数据统计以及看板信息的所有业务逻辑
 * @author: 王春伟
 */
@Service
@Transactional
public class KanBanServiceImpl implements KanBanService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private KanBanDao kanBanDao;
	@Autowired
	private ToolCarDataMapper toolCarDataMapper;

	@Override
	public Map<String, Object> getKanBanInfo(Map<String, Object> reqMap) throws Exception{
		//格式化保留两位小数
		DecimalFormat df = new DecimalFormat("0.00");
		//声明一个用来存储返回结果的map
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//获取所有的工单信息
		List<Map<String, Object>> orderInfoList = kanBanDao.getOrderInfoList(reqMap);
		List<Map<String, Object>> todayYuYueOrderInfoList = null;
		List<Map<String, Object>> todayChuGongOrderInfoList = null;
		if (orderInfoList != null && orderInfoList.size() > 0) {
			//获得今日预约工单
			todayYuYueOrderInfoList = orderInfoList.stream().filter(map -> {
				if (reqMap.get("day").toString().equals(map.get("create_time").toString().substring(0,10))) {
					return true;
				}else {
					return false;
				}

			}).collect(Collectors.toCollection(ArrayList :: new));
			if (todayYuYueOrderInfoList != null && todayYuYueOrderInfoList.size() > 0) {
				//获取今日出工单
				todayChuGongOrderInfoList = todayYuYueOrderInfoList.stream().filter(map -> {
					//如果工单的当前状态大于工单修工出工时的状态,则就为今日出工单
					if (Integer.valueOf(map.get("order_status").toString()) > Constants.ORDER_STATUS_QWXC) {
						return true;
					}else {
						return false;
					}

				}).collect(Collectors.toList());
				//将今日预约单数量加入结果集
				resultMap.put("todayYuYueCount", todayYuYueOrderInfoList.size());
			}else{
				resultMap.put("todayYuYueCount", 0);
			}
			if (todayChuGongOrderInfoList != null && todayChuGongOrderInfoList.size() > 0) {
				//将今日出工单数量加入结果集
				resultMap.put("todayChuGongCount", todayChuGongOrderInfoList.size());

			}else{
				resultMap.put("todayChuGongCount", 0);
			}
			//将接单量加入结果集
			resultMap.put("orderInfoCount", orderInfoList.size());
			//获取返修工单
			List<Map<String, Object>> fanxiuOrderInfoList = orderInfoList.stream().filter(map ->{

				//工单类型为4的即为返修工单
				if (Integer.valueOf(map.get("order_type").toString()) == Constants.ORDER_TYEP_FX) {
					return true;
				}else {
					return false;
				}

			}).collect(Collectors.toCollection(ArrayList :: new));

			if (fanxiuOrderInfoList != null && fanxiuOrderInfoList.size() > 0) {
				//将返修率加入结果集
				resultMap.put("fanxiulv", df.format(((double)fanxiuOrderInfoList.size()/orderInfoList.size())*100));
			}else{
				resultMap.put("fanxiulv", 0);
			}

			//平均维修时间部分
			List<Map<String, Object>> orderTimeInfoList = kanBanDao.getOrderTimeInfo(reqMap);
			//遍历每一个工单,求得所有工单花费的总时间(单位为秒)
			Long totalTimeSecond = orderTimeInfoList.stream().mapToLong(item -> {

				Long handle_time = DateUtil.date2TimeStamp(item.get("handle_time").toString(), "yyyy-MM-dd HH:mm:ss");
				Long order_creat_time = DateUtil.date2TimeStamp(item.get("order_creat_time").toString(), "yyyy-MM-dd HH:mm:ss");
				return handle_time-order_creat_time;

			}).reduce(0,(x,y) -> x+y);

			//则每个工单所花费的时间如下(单位为小时)
//			Double averageTime = (double) (totalTimeSecond/orderTimeInfoList.size()/3600);

			resultMap.put("averageTime",BigDecimal.valueOf((float)totalTimeSecond/(float)orderTimeInfoList.size()/3600).setScale(2, RoundingMode.DOWN));

			//计算总故障数
			int count = kanBanDao.getTotalCount();
			//动力系统总数
			int dongliCount = kanBanDao.getDongLiCount(Constants.DICTIONARIES_NAME_DL);
			//液压系统总数
			int yeyaCount = kanBanDao.getYeYaCount(Constants.DICTIONARIES_NAME_YA);
			//电器系统总数
			int dianqiCount = kanBanDao.getDianQiCount(Constants.DICTIONARIES_NAME_DQ);
			//工作装置总数
			int gongzuoCount = kanBanDao.getGongZuoCount(Constants.DICTIONARIES_NAME_GZ);
			//行走装置总数
			int xingzouCount = kanBanDao.getXingZouCount(Constants.DICTIONARIES_NAME_XZ);
			//其它类型总数
			int qitaCount = kanBanDao.getQiTaCount(Constants.DICTIONARIES_NAME_QT);
			//动力系统占比
			resultMap.put("dongliCount", df.format((double)dongliCount/count));
			//液压系统占比
			resultMap.put("yeyaCount", df.format((double)yeyaCount/count));
			//电器系统占比
			resultMap.put("dianqiCount", df.format((double)dianqiCount/count));
			//工作系统占比
			resultMap.put("gongzuoCount", df.format((double)gongzuoCount/count));
			//行走系统占比
			resultMap.put("xingzouCount", df.format((double)xingzouCount/count));
			//其它系统占比
			resultMap.put("qitaCount", df.format((double)qitaCount/count));

			List<Map<String, Object>> todayZhiFuInfoList = null;
			// 获取支付完成的工单
			todayZhiFuInfoList = orderInfoList.stream().filter(map -> {
				// 如果工单的当前状态大于工单修工出工时的状态,则就为今日出工单
				if (Integer.valueOf(map.get("payment_status").toString()) == 2) {
					return true;
				} else {
					return false;
				}

			}).collect(Collectors.toList());

			//获取用信用支付的工单数量
			Long xingyongCount = todayZhiFuInfoList.stream().filter(map ->{

				if (StringUtils.equals(map.get("payment_type").toString(), Constants.PAY_TYPE_XY)) {
					return true;
				}else {
					return false;
				}

			}).count();
			// 将使用信用支付的占比加入结果集
			resultMap.put("xingyongCount", df.format((double) xingyongCount / todayZhiFuInfoList.size() * 100));

			// 获取用现金支付的工单数量
			Long xianjinCount = todayZhiFuInfoList.stream().filter(map -> {

				if (StringUtils.equals(map.get("payment_type").toString(), Constants.PAY_TYPE_XJ)) {
					return true;
				} else {
					return false;
				}

			}).count();

			// 将使用现金支付的占比加入结果集
			resultMap.put("xianjinCount", df.format((double) xianjinCount / todayZhiFuInfoList.size() * 100));

			// 获取在线支付的订单数量
			Long xianshangCount = todayZhiFuInfoList.stream().filter(map -> {

				if (StringUtils.equals(map.get("payment_type").toString(), Constants.PAY_TYPE_WX)
						|| StringUtils.equals(map.get("payment_type").toString(), Constants.PAY_TYPE_ZFB)) {
					return true;
				} else {
					return false;
				}

			}).count();

			// 将使用在线支付的占比加入结果集
			resultMap.put("xianshangCount", df.format((double) xianshangCount / todayZhiFuInfoList.size() * 100));


			// 声明一个用来存储返回结果的List
			List<Map<String, Object>> resultList = new LinkedList<>();
			// 获取当天的日期
			LocalDate date = LocalDate.now();
			// 默认近30天
			LocalDate date2 = date.minusDays(29);
			// 获取近30天所有的工单
			resultList = orderInfoList.parallelStream().filter(item -> {

				long startTime = DateUtil.date2TimeStamp(date2.toString(), "yyyy-MM-dd");
				long time = DateUtil.date2TimeStamp(item.get("order_creat_time").toString().substring(0, 10),
						"yyyy-MM-dd");
				return time >= startTime;

			}).collect(Collectors.toCollection(ArrayList::new));
			List<Map<String, Object>> trueList = new LinkedList<Map<String, Object>>();
			List<Map<String, Object>> falseList = new LinkedList<Map<String, Object>>();
			// 循环获取每天的完成未完成工单
			for (int i = 0; i <= 29; i++) {
				Map<String, Object> trueMap = new HashMap<String, Object>();
				Map<String, Object> falseMap = new HashMap<String, Object>();
				String day = date2.plusDays(i).toString();
				int trueCount = 0;
				int falseCount = 0;
				for (Map<String, Object> map : resultList) {
					// 获取已完成的工单
					if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_DPJ
							&& StringUtils.equals(day, map.get("update_time").toString().substring(0, 10))) {
						trueCount = trueCount + 1;
						// 获取未完成的工单
					} else if (StringUtils.equals(day, map.get("order_creat_time").toString().substring(0, 10))) {
						falseCount = falseCount + 1;
					}
				}

				// 完成的工单
//				trueMap.put("day",day);
//				trueMap.put("trueCount",trueCount);
				trueMap.put(day, trueCount);
				// 未完成的工单
//				falseMap.put("day",day);
//				falseMap.put("falseCount",falseCount);
				falseMap.put(day, falseCount);
				trueList.add(trueMap);
				falseList.add(falseMap);
			}
			Map<String, Object> groupList = new HashMap<String, Object>();
			groupList.put("true", trueList);
			groupList.put("false", falseList);
			// 将完成率统计部分加入结果集
			resultMap.put("groupMap", groupList);

			//配件销量统计
			//首先获取所有工单明细中的配件信息
			List<Map<String, Object>> orderInfoAndPartInfoList = kanBanDao.getOrderInfoAndPartInfo(reqMap);

			System.out.println("==================================================");

			//根据配件的名称进行分组
			Map<String, List<Map<String, Object>>> partsMap = orderInfoAndPartInfoList.stream().collect(Collectors.groupingBy(map -> {

				if (Optional.ofNullable(map.get("dictionaries_name")).isPresent()) {

					return map.get("dictionaries_name").toString();

				}else {

					return "数据库中字段无配件名称,我这段代码逻辑没问题";

				}

			}));

			System.out.println(partsMap);
			System.out.println("==================================================");
			//声明一个list集合,用来装各个配件的销量
			List<Map<String,Object>> partsList = new ArrayList<>();

			partsMap.entrySet().forEach(item ->{

				Map<String,Object> map = new HashMap<>();

				map.put(item.getKey(), item.getValue().stream().mapToInt(x -> {

					return Integer.valueOf(x.get("quantity").toString());

				}).reduce(0,(x,y) -> x+y));

				partsList.add(map);

			});
//			Collections.sort(partsList,  new Comparator<Map<String, Object>>() {
//				public int compare(Map<String, Object> o1, Map<String, Object> o2) {
//					Integer name1 = Integer.valueOf(o1.get("quantity").toString()) ;//name1是从你list里面拿出来的一个
//					Integer name2 = Integer.valueOf(o2.get("quantity").toString()) ; //name1是从你list里面拿出来的第二个name
//					return name2.compareTo(name1);
//				}
//			});
			//将每一个配件的销量加入结果集
			resultMap.put("partsList", partsList);
			System.out.println(partsList);


			//修工部分
			//查询对应修工所负责的所有工单(此处需传修工的id给我,否则报空指针)
			reqMap.put("tsod_status",13);
			List<Map<String, Object>> xgOrderInfoList = kanBanDao.getOrderInfoListByXgIdAndStatus(reqMap);

			List<Map<String, Object>> zxgList = xgOrderInfoList.stream().filter(t -> (int)t.get("u_type")==1 ).collect(Collectors.toList());
			List<Map<String, Object>> fxgList = xgOrderInfoList.stream().filter(t -> (int)t.get("u_type")==2 ).collect(Collectors.toList());

			List xgList=new ArrayList();
			List<Map<String, Object>> zoptionalList = Optional.ofNullable(zxgList).orElseGet(ArrayList :: new);
			List<Map<String, Object>> foptionalList = Optional.ofNullable(fxgList).orElseGet(ArrayList :: new);
			//接单量占比
			//将接单量占比加入结果集
//			resultMap.put("jiedanlv", df.format(jiedanlv));
			Map<String,Object> xgjdmap=new HashMap<>();
			xgjdmap.put("item","接单量");
			xgjdmap.put("主修",BigDecimal.valueOf(((float) zoptionalList.size()/(float)orderInfoList.size())*100).setScale(2, RoundingMode.DOWN));
			xgjdmap.put("辅修",BigDecimal.valueOf(((float)foptionalList.size()/(float)orderInfoList.size())*100).setScale(2, RoundingMode.DOWN));
			xgList.add(xgjdmap);
			//好评的单子数量
			Long haopingCount = zoptionalList.stream().filter(map -> {

				Integer appraise_manner =Integer.valueOf(Optional.ofNullable(map.get("appraise_manner")).orElse("0").toString());
				Integer appraise_result =Integer.valueOf(Optional.ofNullable(map.get("appraise_result")).orElse("0").toString());
				Integer appraise_productivity =Integer.valueOf(Optional.ofNullable(map.get("appraise_productivity")).orElse("0").toString());
				return appraise_manner+appraise_result+appraise_productivity >= 9;

			}).count();

			Long fxhaopingCount = foptionalList.stream().filter(map -> {

				Integer appraise_manner =Integer.valueOf(Optional.ofNullable(map.get("appraise_manner")).orElse("0").toString());
				Integer appraise_result =Integer.valueOf(Optional.ofNullable(map.get("appraise_result")).orElse("0").toString());
				Integer appraise_productivity =Integer.valueOf(Optional.ofNullable(map.get("appraise_productivity")).orElse("0").toString());
				return appraise_manner+appraise_result+appraise_productivity >= 9;

			}).count();

//			System.out.println(optionalList.size());
			//好评占比
//			Double haopinglv = zoptionalList.size() == 0 ? 0.0 :(float)haopingCount/(float)zoptionalList.size();
			//将好评率加入结果集
//			resultMap.put("haopinglv", df.format(haopinglv));

			Map<String,Object> xghpmap=new HashMap<>();
			xghpmap.put("item","好评");
			xghpmap.put("主修",BigDecimal.valueOf((zoptionalList.size() == 0 ? 0.0 :(float)haopingCount/(float)zoptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xghpmap.put("辅修",BigDecimal.valueOf((foptionalList.size() == 0 ? 0.0 : (float) fxhaopingCount/(float)foptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgList.add(xghpmap);
			//计算达标的单子的数量(即没有返修的单子):
			Long zdabiaoCount = zoptionalList.stream().filter(map -> {
				if (Integer.valueOf(Optional.ofNullable(map.get("order_type")).orElse("0").toString()) != 4) {
					return true;
				}else {
					return false;
				}
			}).count();
			Long fdabiaoCount = foptionalList.stream().filter(map -> {
				if (Integer.valueOf(Optional.ofNullable(map.get("order_type")).orElse("0").toString()) != 4) {
					return true;
				}else {
					return false;
				}
			}).count();
			//达标占比

//			Double dabiaolv = zoptionalList.size() == 0 ? 0.0 : (float)zdabiaoCount/(float)zoptionalList.size();

			//将达标占比加入结果集
//			resultMap.put("dabiaolv", df.format(dabiaolv));
			Map<String,Object> xgdbmap=new HashMap<>();
			xgdbmap.put("item","达标率");
			xgdbmap.put("主修",BigDecimal.valueOf( zoptionalList.size() == 0 ? 0.0 : ((float)zdabiaoCount/(float)zoptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgdbmap.put("辅修",BigDecimal.valueOf(foptionalList.size() == 0 ? 0.0 : ((float)fdabiaoCount/(float)foptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgList.add(xgdbmap);

			//计算维修比
			Long zweixiuCount = zoptionalList.stream().filter(map -> {
				if (Integer.valueOf(Optional.ofNullable(map.get("order_type")).orElse("0").toString()) == 1 || Integer.valueOf(Optional.ofNullable(map.get("order_type")).orElse("0").toString()) == 3) {
					return true;
				}else {
					return false;
				}
			}).count();
			Long fweixiuCount = foptionalList.stream().filter(map -> {
				if (Integer.valueOf(Optional.ofNullable(map.get("order_type")).orElse("0").toString()) == 1 || Integer.valueOf(Optional.ofNullable(map.get("order_type")).orElse("0").toString()) == 3) {
					return true;
				}else {
					return false;
				}
			}).count();
			//维修占比
//			Double zweixiuzhanbi = zoptionalList.size() == 0 ? 0.0 : (float)zweixiuCount/(float)zoptionalList.size();
//			Double fweixiuzhanbi = foptionalList.size() == 0 ? 0.0 : (float)fweixiuCount/(float)foptionalList.size();
			//将维修占比加入结果集
//			resultMap.put("weixiuzhanbi", df.format(weixiuzhanbi));
			Map<String,Object> xgwxmap=new HashMap<>();
			xgwxmap.put("item","维修比");
			xgwxmap.put("主修",BigDecimal.valueOf( zoptionalList.size() == 0 ? 0.0 : ((float)zweixiuCount/(float)zoptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgwxmap.put("辅修",BigDecimal.valueOf(foptionalList.size() == 0 ? 0.0 : ((float)fweixiuCount/(float)foptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgList.add(xgwxmap);
			//计算维修效率(即已完成的单除以总单数)
			//已完成的单子的数量			//			zoptionalList foptionalList
			Long zfinishedOrderCount = zoptionalList.stream().filter(map -> {
				if (Integer.valueOf(Optional.ofNullable(map.get("order_status")).orElse("0").toString()) == Constants.ORDER_STATUS_WC) {
					return true;
				}else {
					return false;
				}
			}).count();
			Long ffinishedOrderCount = foptionalList.stream().filter(map -> {
				if (Integer.valueOf(Optional.ofNullable(map.get("order_status")).orElse("0").toString()) == Constants.ORDER_STATUS_WC) {
					return true;
				}else {
					return false;
				}
			}).count();
			//计算维修效率
//			Double zweixiulv = zoptionalList.size() == 0 ? 0.0 : (float)zfinishedOrderCount/(float)zoptionalList.size();
//			Double fweixiulv = foptionalList.size() == 0 ? 0.0 : (float)ffinishedOrderCount/(float)foptionalList.size();
			//将维修效率加入结果集
//			resultMap.put("weixiulv", df.format(weixiulv));
			Map<String,Object> xgxlmap=new HashMap<>();
			xgxlmap.put("item","维修效率");
			xgxlmap.put("主修",BigDecimal.valueOf(zoptionalList.size() == 0 ? 0.0 : ((float)zfinishedOrderCount/(float)zoptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgxlmap.put("辅修",BigDecimal.valueOf(foptionalList.size() == 0 ? 0.0 : ((float)ffinishedOrderCount/(float)foptionalList.size())*100).setScale(2, RoundingMode.DOWN));
			xgList.add(xgxlmap);
			resultMap.put("xglist",xgList);
			return resultMap;
		}else {
			return null;
		}
	}
	/**
	 * 营收日统计
	 */
	@Override
	public Map<String, Object> getYingShouInfo(Map<String, Object> reqMap) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 按序列排序
		List<Map<String, Object>> yingShouList = new LinkedList<Map<String, Object>>();
		Map<String,Object> result=new HashMap<>();
		// 获取所有的工单信息

		reqMap.put("creatTime",sdf.format(new Date()));
		//获取今日工单
		List<Map<String, Object>> orderInfoList = kanBanDao.getToDayOrderInfoList(reqMap);
//		List<Map<String, Object>> orderInfoList = kanBanDao.getOrderInfoList(reqMap);
		for (Date day : DateUtil.DateList()) {

			// 声明一个用来存储返回结果的map
			Map<String, Object> resultMap = new HashMap<String, Object>();
			List<Map<String, Object>> todayOrderInfoList = null;
			List<Map<String, Object>> todayYuYueOrderInfoList = null;
			List<Map<String, Object>> todayChuGongOrderInfoList = null;
			Double sumPrice = 0.0;
			Long noAccount = 0L;
			Long finishCount =0L;
			Long unfinishCount =0L;
			if (orderInfoList != null && orderInfoList.size() > 0) {
				// 获得今日工单
				todayOrderInfoList = orderInfoList.stream().filter(map -> {
					if (sdf.format(day).toString().equals(map.get("create_time").toString().substring(0, 10))) {
						return true;
					} else {
						return false;
					}

				}).collect(Collectors.toCollection(ArrayList::new));
				if (todayOrderInfoList != null && todayOrderInfoList.size() > 0) {
					// 获取今日预约工单
					todayYuYueOrderInfoList = todayOrderInfoList.stream().filter(map -> {
						// 如果工单的当前状态大于工单修工出工时的状态,则就为今日出工单
						if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_DSL) {
							return true;
						} else {
							return false;
						}

					}).collect(Collectors.toList());
					// 获取今日出工单
					todayChuGongOrderInfoList = todayOrderInfoList.stream().filter(map -> {
						// 如果工单的当前状态大于工单修工出工时的状态,则就为今日出工单
						if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_YLL) {
							return true;
						} else {
							return false;
						}

					}).collect(Collectors.toList());

					// 今日已结账
					sumPrice = todayOrderInfoList.stream().mapToDouble(item -> {
						// 判空,如果为null,则给默认值0.0(此处为double类型)
						if((int)item.get("payment_status")!=2){
							return 0.0;
						}
						Object optional = Optional.ofNullable(item.get("payment_amount")).orElseGet(() -> {

							return "0.0";

						});

						Double sum = Double.valueOf(optional.toString());

						return sum;

					}).reduce(0.0, (x, y) -> x + y);

					// 今日未结账
					noAccount = todayOrderInfoList.stream().filter(item -> {

						return item.get("payment_amount") == null;

					}).count();

					// 今日完成工单量
					finishCount = todayOrderInfoList.stream().filter(item -> {
						Object optional = null;
						try {
							optional = Optional.ofNullable(item.get("order_status")).orElseThrow(Exception::new);
						} catch (Exception e) {
							return false;
						}
						return Integer.valueOf(optional.toString()) >= Constants.ORDER_STATUS_DPJ;

					}).count();

					// 今日未完成工单
					unfinishCount = todayOrderInfoList.stream().filter(item -> {
						Object optional = null;
						try {
							optional = Optional.ofNullable(item.get("order_status")).orElseThrow(Exception::new);
						} catch (Exception e) {
							return false;
						}
						return Integer.valueOf(optional.toString()) != Constants.ORDER_STATUS_WC && Integer.valueOf(optional.toString()) != Constants.ORDER_STATUS_DPJ;

					}).count();


				}

				resultMap.put("time",sdf.format(day));
				// 将今日预约单数量加入结果集
				resultMap.put("todayYuYueCount",(todayYuYueOrderInfoList != null && todayYuYueOrderInfoList.size() > 0)?todayYuYueOrderInfoList.size():0);
				// 将今日出工单数量加入结果集
				resultMap.put("todayChuGongCount", (todayChuGongOrderInfoList != null && todayChuGongOrderInfoList.size() > 0)?todayChuGongOrderInfoList.size():0);
				// 将今日已结账的总金额加入结果集
				resultMap.put("todayAccount", BigDecimal.valueOf(sumPrice).setScale(2, RoundingMode.DOWN));
				// 将今日未结账的工单数量加入结果集
				resultMap.put("noAccount", noAccount);
				// 今日完成工单量
				resultMap.put("finishCount", finishCount);
				// 今日未完成工单
				resultMap.put("unfinishCount", unfinishCount);
				// 今日维修工单的数量
				resultMap.put("todayWeixiuCount", weiXiOrBaoyangCount(todayOrderInfoList).get("yuWeiXi"));
				// 今日保养工单的数量
				resultMap.put("todayWeixiuCount", weiXiOrBaoyangCount(todayOrderInfoList).get("yuBaoYang"));
				if(sdf.format(day).equals(reqMap.get("creatTime"))){
					result.put("today",resultMap);
				}
				yingShouList.add(resultMap);
			}else {
				resultMap.put("time",sdf.format(day));
				// 将今日预约单数量加入结果集
				resultMap.put("todayYuYueCount",(todayYuYueOrderInfoList != null && todayYuYueOrderInfoList.size() > 0)?todayYuYueOrderInfoList.size():0);
				// 将今日出工单数量加入结果集
				resultMap.put("todayChuGongCount", (todayChuGongOrderInfoList != null && todayChuGongOrderInfoList.size() > 0)?todayChuGongOrderInfoList.size():0);
				// 将今日已结账的总金额加入结果集
				resultMap.put("todayAccount", BigDecimal.valueOf(sumPrice).setScale(2, RoundingMode.DOWN));
				// 将今日未结账的工单数量加入结果集
				resultMap.put("noAccount", noAccount);
				// 今日完成工单量
				resultMap.put("finishCount", finishCount);
				// 今日未完成工单
				resultMap.put("unfinishCount", unfinishCount);
				// 今日维修工单的数量
				resultMap.put("todayWeixiuCount", weiXiOrBaoyangCount(todayOrderInfoList).get("yuWeiXi"));
				// 今日保养工单的数量
				resultMap.put("todayWeixiuCount", weiXiOrBaoyangCount(todayOrderInfoList).get("yuBaoYang"));
				if(sdf.format(day).equals(reqMap.get("creatTime"))){
					result.put("today",resultMap);
				}
				yingShouList.add(resultMap);
			}


		}
		result.put("yingShouList",yingShouList);
		return result;

	}
	/**
	 * 营收月统计
	 */
	@Override
	public Map<String, Object> getYueYingShouInfo(Map<String, Object> reqMap) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String yes=sdf.format(new Date());
		List<String> monthList=new ArrayList<>();
		for(int i=1;i<13;i++){
			String mont=yes;
			if(i<10){
				mont+="-0"+i;
			}else{
				mont+="-"+i;
			}
			monthList.add(mont);
		}
//		List<String> monthList = JSONObject.parseObject(reqMap.get("monthList").toString(), List.class);
//		Assert.notNull(monthList, () -> {
//			return "月份不能为空";
//		});
		// 获取所有的工单信息
		List<Map<String, Object>> orderInfoList = kanBanDao.getOrderInfoList(reqMap);
		Map<String,Object> result=new HashMap<>();
		// 按序列排序
		List<Map<String, Object>> yueYingShouList = new LinkedList<Map<String, Object>>();
		for (String month : monthList) {
			// 声明一个用来存储返回结果的map
			Map<String, Object> resultMap = new HashMap<String, Object>();
			List<Map<String, Object>> monthOrderInfoList = null;
			List<Map<String, Object>> monthYuYueOrderInfoList = null;
			List<Map<String, Object>> monthChuGongOrderInfoList = null;
			Double sumPrice = 0.0;
			Long noAccount = 0L;
			Long finishCount =0L;
			Long unfinishCount =0L;
			if (orderInfoList != null && orderInfoList.size() > 0) {
				// 获得本月工单
				monthOrderInfoList = orderInfoList.stream().filter(map -> {
					if (month.equals(map.get("create_time").toString().substring(0, 7))) {
						return true;
					} else {
						return false;
					}

				}).collect(Collectors.toCollection(ArrayList::new));
				if (monthOrderInfoList != null && monthOrderInfoList.size() > 0) {
					// 获取本月预约工单
					monthChuGongOrderInfoList = monthOrderInfoList.stream().filter(map -> {
						// 如果工单的当前状态大于工单修工出工时的状态,则就为本月出工单
						if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_DSL) {
							return true;
						} else {
							return false;
						}

					}).collect(Collectors.toList());
					// 获取本月出工单
					monthChuGongOrderInfoList = monthOrderInfoList.stream().filter(map -> {
						// 如果工单的当前状态大于工单修工出工时的状态,则就为本月出工单
						if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_YLL) {
							return true;
						} else {
							return false;
						}

					}).collect(Collectors.toList());

					// 本月已结账
					sumPrice = monthOrderInfoList.stream().mapToDouble(item -> {
						if((int)item.get("payment_status")!=2){
							return 0.0;
						}
						// 判空,如果为null,则给默认值0.0(此处为double类型)
						Object optional = Optional.ofNullable(item.get("payment_amount")).orElseGet(() -> {

							return "0.0";

						});

						Double sum = Double.valueOf(optional.toString());

						return sum;

					}).reduce(0.0, (x, y) -> x + y);

					// 本月未结账
					noAccount = monthOrderInfoList.stream().filter(item -> {

						return item.get("payment_amount") == null;

					}).count();

					// 本月完成工单量
					finishCount = monthOrderInfoList.stream().filter(item -> {
						Object optional = null;
						try {
							optional = Optional.ofNullable(item.get("order_status")).orElseThrow(Exception::new);
						} catch (Exception e) {
							return false;
						}
						return Integer.valueOf(optional.toString()) >= Constants.ORDER_STATUS_DPJ;

					}).count();

					// 本月未完成工单
					unfinishCount = monthOrderInfoList.stream().filter(item -> {
						Object optional = null;
						try {
							optional = Optional.ofNullable(item.get("order_status")).orElseThrow(Exception::new);
						} catch (Exception e) {
							return false;
						}
						return Integer.valueOf(optional.toString()) != Constants.ORDER_STATUS_DPJ && Integer.valueOf(optional.toString()) != Constants.ORDER_STATUS_WC;

					}).count();

				}
				// 将本月预约单数量加入结果集
				resultMap.put("monthYuYueCount",(monthOrderInfoList != null && monthOrderInfoList.size() > 0)?monthOrderInfoList.size():0);
				// 将本月出工单数量加入结果集
				resultMap.put("monthChuGongCount", (monthChuGongOrderInfoList != null && monthChuGongOrderInfoList.size() > 0)?monthChuGongOrderInfoList.size():0);
				// 将本月已结账的总金额加入结果集
				resultMap.put("monthAccount", BigDecimal.valueOf(sumPrice).setScale(2, RoundingMode.DOWN));
				// 将本月未结账的工单数量加入结果集
				resultMap.put("noAccount", noAccount);
				// 本月完成工单量
				resultMap.put("finishCount", finishCount);
				// 本月未完成工单
				resultMap.put("unfinishCount", unfinishCount);
				// 本月维修工单的数量
				resultMap.put("monthWeixiuCount", weiXiOrBaoyangCount(monthOrderInfoList).get("yuWeiXi"));
				// 本月保养工单的数量
				resultMap.put("monthWeixiuCount", weiXiOrBaoyangCount(monthOrderInfoList).get("yuBaoYang"));
				resultMap.put("month",month);
				if(month.equals(sdf.format(new Date()))){
					result.put("thisMonth",resultMap);
				}
				yueYingShouList.add(resultMap);
			}
		}
		result.put("yueYingShouList",yueYingShouList);
		return result;

	}

	/**
	 * 周营收统计
	 */
	@Override
	public List<Map<String, Object>> getWeekYingShouInfo(Map<String, Object> reqMap) throws Exception {

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		List<String> monthList=new ArrayList<>();
		Map<String,Object> result=new HashMap<>();
		Calendar c = Calendar.getInstance();
		//第一天
		c.set(Calendar.DAY_OF_MONTH,1);
		System.out.println(sdf.format(c.getTime()));
		for(int n=1; n<5;n++){
			c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
			String start=sdf.format(c.getTime());
			c.add(Calendar.DATE, 7);
			c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
			String end=sdf.format(c.getTime());
			monthList.add(start+"~"+end);
		}


//		 monthList = JSONObject.parseObject(reqMap.get("weekList").toString(), List.class);
//		Assert.notNull(monthList, () -> {
//			return "周不能为空";
//		});
		// 获取所有的工单信息
		List<Map<String, Object>> orderInfoList = kanBanDao.getOrderInfoList(reqMap);
		// 按序列排序
		List<Map<String, Object>> weekYingShouList = new LinkedList<Map<String, Object>>();
		for (String week : monthList) {
			String startDate = week.split("~")[0];
			String endDate =week.split("~")[1];
			// 声明一个用来存储返回结果的map
			Map<String, Object> resultMap = new HashMap<String, Object>();
			List<Map<String, Object>> weekOrderInfoList = null;
			List<Map<String, Object>> weekYuYueOrderInfoList = null;
			List<Map<String, Object>> weekChuGongOrderInfoList = null;
			Double sumPrice = 0.0;
			Long noAccount = 0L;
			Long finishCount =0L;
			Long unfinishCount =0L;
			if (orderInfoList != null && orderInfoList.size() > 0) {
				// 获得本周工单
				weekOrderInfoList = orderInfoList.stream().filter(map -> {
					long start = DateUtil.date2TimeStamp(startDate, "yyyy-MM-dd");
					long end = DateUtil.date2TimeStamp(endDate, "yyyy-MM-dd");
					long orderTime = DateUtil.date2TimeStamp(map.get("create_time").toString(), "yyyy-MM-dd");
					if (start <= orderTime && orderTime <= end) {
						return true;
					} else {
						return false;
					}

				}).collect(Collectors.toCollection(ArrayList::new));
				if (weekOrderInfoList != null && weekOrderInfoList.size() > 0) {
					// 获取本周预约工单
					weekYuYueOrderInfoList = weekOrderInfoList.stream().filter(map -> {
						// 如果工单的当前状态大于工单修工出工时的状态,则就为本周出工单
						if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_DSL) {
							return true;
						} else {
							return false;
						}

					}).collect(Collectors.toList());
					// 获取本周出工单
					weekChuGongOrderInfoList = weekOrderInfoList.stream().filter(map -> {
						// 如果工单的当前状态大于工单修工出工时的状态,则就为本周出工单
						if (Integer.valueOf(map.get("order_status").toString()) >= Constants.ORDER_STATUS_YLL) {
							return true;
						} else {
							return false;
						}

					}).collect(Collectors.toList());

					// 本周已结账
					sumPrice = weekOrderInfoList.stream().mapToDouble(item -> {
						if((int)item.get("payment_status")!=2){
							return 0.0;
						}
						// 判空,如果为null,则给默认值0.0(此处为double类型)
						Object optional = Optional.ofNullable(item.get("payment_amount")).orElseGet(() -> {

							return "0.0";

						});

						double sum = Double.valueOf(optional.toString());

						return sum;

					}).reduce(0.0, (x, y) -> x + y);

					// 本周未结账
					noAccount = weekOrderInfoList.stream().filter(item -> {

						return item.get("payment_amount") == null;

					}).count();

					// 本周完成工单量
					finishCount = weekOrderInfoList.stream().filter(item -> {
						Object optional = null;
						try {
							optional = Optional.ofNullable(item.get("order_status")).orElseThrow(Exception::new);
						} catch (Exception e) {
							return false;
						}
						return Integer.valueOf(optional.toString()) == Constants.ORDER_STATUS_WC;

					}).count();

					// 本周未完成工单
					unfinishCount = weekOrderInfoList.stream().filter(item -> {
						Object optional = null;
						try {
							optional = Optional.ofNullable(item.get("order_status")).orElseThrow(Exception::new);
						} catch (Exception e) {
							return false;
						}
						return Integer.valueOf(optional.toString()) != Constants.ORDER_STATUS_WC;

					}).count();
				}
				//  将本周预约单数量加入结果集
				resultMap.put("weekYuYueCount",(weekYuYueOrderInfoList != null && weekYuYueOrderInfoList.size() > 0)?weekYuYueOrderInfoList.size():0);
				// 将本周出工单数量加入结果集
				resultMap.put("weekChuGongCount", (weekChuGongOrderInfoList != null && weekChuGongOrderInfoList.size() > 0)?weekChuGongOrderInfoList.size():0);
				// 将本周已结账的总金额加入结果集
				resultMap.put("weekAccount", BigDecimal.valueOf(sumPrice).setScale(2, RoundingMode.DOWN));
				// 将本周未结账的工单数量加入结果集
				resultMap.put("noAccount", noAccount);
				// 本周完成工单量
				resultMap.put("finishCount", finishCount);
				// 本周未完成工单
				resultMap.put("unfinishCount", unfinishCount);
				//本周维修工单量
				resultMap.put("weekWeixiuCount", weiXiOrBaoyangCount(weekOrderInfoList).get("yuWeiXi"));
				//本周保养工单量
				resultMap.put("weekWeixiuCount", weiXiOrBaoyangCount(weekOrderInfoList).get("yuBaoYang"));
				resultMap.put("date",startDate+"~"+endDate);
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
				if(startDate.equals(sdf.format(c.getTime()))){
					result.put("weekYingShou",resultMap);
				}

				weekYingShouList.add(resultMap);
			}
		}
		result.put("weekYingShouList",weekYingShouList);
		return weekYingShouList;
	}
	@Override
	@SuppressWarnings({"unchecked"})
	public List<Map<String, Object>> getPartsPridicateInfo(Map<String, Object> reqMap) throws Exception {
		// 声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
		// 获取所有的工单明细配件详细信息
		// 获取当天的日期
		LocalDate date = LocalDate.now();
		// 默认近30天
		LocalDate date2 = date.minusDays(29);
		reqMap.put("LocalDate", java.sql.Date.valueOf(date2));
		// 获取除了减去的所有配件
		List<Map<String, Object>> htcList = kanBanDao.getOrderdetailPartsInfoListT(reqMap);
		// 获取所有的减去配件
		List<Map<String, Object>> cutList = kanBanDao.getCutPartsInfoList(reqMap);
		for (Map<String, Object> htc : htcList) {
			Map<String, Object> partsInfoMap = new HashMap<String, Object>();
			partsInfoMap.put("dictionaries_name", htc.get("dictionaries_name"));
			partsInfoMap.put("dictionaries_model", htc.get("dictionaries_model"));
			int quantity = Integer.valueOf(htc.get("quantity").toString());
			if (cutList != null && cutList.size() > 0) {
				// 循环进行加减配件
				for (Map<String, Object> cut : cutList) {
					if (StringUtils.equals(htc.get("dictionaries_name").toString(),
							cut.get("dictionaries_name").toString())) {
						quantity = quantity - Integer.valueOf(cut.get("quantity").toString());
						break;
					}
				}
			}
			partsInfoMap.put("quantity", quantity/4);
			resultList.add(partsInfoMap);
		}
		return resultList;
//		//声明一个用来存储返回结果的List
//		List<Map<String, Object>> resultList = null;
//		//获取所有的车辆信息列表
//		List<Map<String, Object>> carInfoList = kanBanDao.getCarInfoList(reqMap);
//		//获取保养套餐列表
//		List<Map<String, Object>> upkeepInfoList = kanBanDao.getUpkeepInfoList(reqMap);
//		//根据s_id查询服务商信息
//		Map<String, Object> serverInfo = kanBanDao.getServerInfoBySId(reqMap);
//		Map<String, Double> doubleMap = new HashMap<>();
////		List<String> upkeepIdList = carInfoList.parallelStream().map(item ->{
//		List<String> upkeepIdList=new ArrayList<>();
//		for(Map<String, Object> item:carInfoList){
//
//
//			try {
//				//调用物联网卡地址接口查询信息
//				String thingsInfo = HttpClient.doGet(Constants.THINGS_URL_ONE + item.get("opc_path"),serverInfo.get("interface_account") + "", serverInfo.get("unify_account") + "");
//				//获取该车辆注册后运行的工时
//				List<Map<String,Object>> list =JSONObject.parseObject(thingsInfo, List.class);
//				//新思联接口获取车辆注册后的运行工时待计算
//				list.forEach(dataTime -> {
//					List<Map<String,Object>> valueList = JSONObject.parseObject(dataTime.get("OPCValueList").toString(),List.class);
//					valueList.forEach(m ->{
//
//						if ("运行小时".equals(m.get("ValueName").toString())) {
//
//							doubleMap.put("runHours", Double.valueOf(m.get("DataValue").toString()));
//
//						}
//
//					});
//				});
//				//计算车辆的总运行工时
//				double sum=0;
//				if(doubleMap.size()>0){
//					sum = Double.valueOf(item.get("running").toString()) + doubleMap.get("runHours");
//				}else{
//					sum = Double.valueOf(item.get("running").toString());
//				}
//				for(Map<String,Object> map:upkeepInfoList){
//					double upkeep = Double.valueOf(map.get("upkeep_time").toString());
//					if(upkeep >= sum ){
////						if(upkeep >= sum && item.get("model").toString().equals(map.get("model").toString())
////								&& item.get("brand").toString().equals(map.get("brand").toString())){
//						upkeepIdList.add(map.get("id").toString());
//					}
//				}
//			} catch (NumberFormatException e) {
//				e.printStackTrace();
//			}
//			//获取每一辆车所对应的最适合的保养套餐的主键id
////			Optional<String> id = upkeepInfoList.parallelStream().filter(x ->{
////				//获取保养套餐列表中的所有保养时长
////				double upkeep = Double.valueOf(x.get("upkeep_time").toString());
////				//筛选出对应车辆品牌,型号,车型的车辆
////				return upkeep >= sum && item.get("model").toString().equals(x.get("model").toString())
////
////						&& item.get("brand").toString().equals(x.get("brand").toString())
////						&& item.get("model").toString().equals(x.get("model").toString());
////
////			}).sorted((o1,o2) ->{//按保养时长进行升序排序
////
////				return o1.get("upkeep_time").toString().compareTo(o2.get("upkeep_time").toString());
////
////			}).findFirst().map(y ->{
////
////				return y.get("id").toString();
////
////			});
////			try {
////				//如果不存在则抛出异常
////				return id.orElseThrow(Exception :: new);
////
////			} catch (Exception e) {
////				e.printStackTrace();
////				return "出错了,获取不到对应车辆适合的保养套餐";
////
////			}
//
////		}).collect(Collectors.toCollection(ArrayList :: new));
//			//获取保养配件相关的关联信息
//		}
//		List<Map<String, Object>> mapList = new ArrayList<>();
//		try {
//			for(String item:upkeepIdList){
//				reqMap.put("upkeep_dictionaries_id", item);
//				//查询数据库
//				List<Map<String, Object>> map = kanBanDao.getUpkeepDictionariesInfo(reqMap);
//				if(null!=map&&map.size()>0){{
//					mapList.add(map.get(0));
//				}
//				}
//			}
////			mapList = upkeepIdList.parallelStream().map(item ->{
////				//先根据保养套餐id查询保养时长配件相因的信息
////				reqMap.put("upkeep_dictionaries_id", item);
////				//查询数据库
////				List<Map<String, Object>> map = kanBanDao.getUpkeepDictionariesInfo(reqMap);
////				//返回保养配件相关的关联信息
////				return map.get(0);
////			}).collect(Collectors.toCollection(ArrayList:: new));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		if (mapList.size() == 0) {
//			return resultList;
//		}
//		//根据配件名称进行分组,然后再按配件型号在配件名称的基础上进行分组(即进行多级分组)
//		Map<String, Map<String, List<Map<String, Object>>>> partsMap = mapList.parallelStream().collect(Collectors.groupingBy(map -> {
//
//			if (Optional.ofNullable(map.get("dictionaries_name")).isPresent()) {
//
//				return map.get("dictionaries_name").toString();
//
//			}else {
//
//				return "我是管理员给的默认名称配件";
//
//			}
//
//		},Collectors.groupingBy(map -> {
//
//			if (Optional.ofNullable(map.get("dictionaries_model")).isPresent()) {
//
//				return map.get("dictionaries_model").toString();
//
//			}else {
//
//				return "默认型号";
//
//			}
//
//		})));
//		//统计各配件的结果数量
//		resultList =partsMap.entrySet().parallelStream().map(item ->{
//			//声明集合容器
//			Map<String,Object> map = new HashMap<>();
//			//获取同一名称的保养配件的总数量
//			item.getValue().entrySet().stream().forEach(a1 ->{
//				//统计对应配件对应名称对应型号的数量.
//				int quantity = a1.getValue().stream().mapToInt(x ->{
//
//					return Integer.valueOf(x.get("quantity").toString());
//
//				}).reduce(0, (m,n) -> m+n);
//				//封装结果集
//				map.put("dictionaries_name", item.getKey());
//				map.put("dictionaries_model", a1.getKey());
//				map.put("quantity", quantity);
//
//			});
//
//			return map;
//
//		}).collect(Collectors.toCollection(ArrayList :: new));
//
//		return resultList;

	}
	@Override
	public List<Map<String, Object>> getPartsSalesInfo(Map<String, Object> reqMap) throws Exception {

		// 声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
		// 获取所有的工单明细配件详细信息
		// 获取当天的日期
		LocalDate date = LocalDate.now();
		// 默认近30天
		LocalDate date2 = date.minusDays(29);
		reqMap.put("LocalDate", java.sql.Date.valueOf(date2));
		// 获取除了减去的所有配件
		List<Map<String, Object>> htcList = kanBanDao.getOrderdetailPartsInfoListT(reqMap);
		// 获取所有的减去配件
		List<Map<String, Object>> cutList = kanBanDao.getCutPartsInfoList(reqMap);
		for (Map<String, Object> htc : htcList) {
			Map<String, Object> partsInfoMap = new HashMap<String, Object>();
			partsInfoMap.put("dictionaries_name", htc.get("dictionaries_name"));
			partsInfoMap.put("dictionaries_model", htc.get("dictionaries_model"));
			int quantity = Integer.valueOf(htc.get("quantity").toString());
			if (cutList != null && cutList.size() > 0) {
				// 循环进行加减配件
				for (Map<String, Object> cut : cutList) {
					if (StringUtils.equals(htc.get("dictionaries_name").toString(),
							cut.get("dictionaries_name").toString())) {
						quantity = quantity - Integer.valueOf(cut.get("quantity").toString());
						break;
					}
				}
			}
			partsInfoMap.put("quantity", quantity);
			resultList.add(partsInfoMap);
		}
		return resultList;

//		//声明一个用来存储返回结果的List
//		List<Map<String, Object>> resultList = null;
//		//获取所有的工单明细配件详细信息
//		List<Map<String, Object>> orderdetailPartsInfoList = kanBanDao.getOrderdetailPartsInfoList(reqMap);
//		//获取当天的日期
//		LocalDate date = LocalDate.now();
//		//默认近30天
//		LocalDate date2 = date.minusDays(30);
//		//获取近30天的所有工单
//		List<Map<String, Object>> orderdetailPartsInfoList1 = orderdetailPartsInfoList.parallelStream().filter(item ->{
//
//			long startTime = DateUtil.date2TimeStamp(date2.toString(), "yyyy-MM-dd");
//			long time = DateUtil.date2TimeStamp(item.get("create_time").toString().substring(0,10), "yyyy-MM-dd");
//			return time >= startTime;
//
//		}).collect(Collectors.toCollection(ArrayList :: new));
//		//根据配件名称进行分组,然后再按配件型号在配件名称的基础上进行分组(即进行多级分组)
//		Map<String, Map<String, List<Map<String, Object>>>> partsMap = orderdetailPartsInfoList1.parallelStream().collect(Collectors.groupingBy(map -> {
//
//			if (Optional.ofNullable(map.get("dictionaries_name")).isPresent()) {
//
//				return map.get("dictionaries_name").toString();
//
//			}else {
//
//				return "我是管理员给的默认名称配件!";
//
//			}
//
//		},Collectors.groupingBy(map -> {
//
//			if (Optional.ofNullable(map.get("dictionaries_model")).isPresent()) {
//
//				return map.get("dictionaries_model").toString();
//
//			}else {
//
//				return "默认型号";
//
//			}
//
//		})));
//		//统计各配件的结果数量
//		resultList =partsMap.entrySet().parallelStream().map(item ->{
//			//声明集合容器
//			Map<String,Object> map = new HashMap<String,Object>();
//			//获取同 一名称的保养配件的总数量
//			item.getValue().entrySet().stream().forEach(a ->{
//				//统计对应配件对应名称对应型号的数量
//				int quantity = a.getValue().stream().mapToInt(x ->{
//
//					return Integer.valueOf(x.get("quantity").toString());
//
//				}).reduce(0, (m,n) -> m+n);
//				//封装结果集
//				map.put("dictionaries_name", item.getKey());
//				map.put("dictionaries_model", a.getKey());
//				map.put("quantity", quantity);
//
//			});
//			return map;
//
//		}).collect(Collectors.toCollection(ArrayList :: new));
//		Collections.sort(resultList,  new Comparator<Map<String, Object>>() {
//			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
//				Integer name1 = Integer.valueOf(o1.get("quantity").toString()) ;//name1是从你list里面拿出来的一个
//				Integer name2 = Integer.valueOf(o2.get("quantity").toString()) ; //name1是从你list里面拿出来的第二个name
//				return name2.compareTo(name1);
//			}
//		});
//		return resultList;

	}
	@Override
	public List<Map<String, Object>> getChaPingInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new ArrayList<>();

		//获取所有的工单明细配件详细信息
		LocalDate date = LocalDate.now();
		//默认近30天
		LocalDate date2 = date.minusDays(30);
		reqMap.put("startTime",date.toString());
		reqMap.put("endTime",date2.toString());
		List<Map<String, Object>> orderdetailPartsInfoList = kanBanDao.getChaPingInfo(reqMap);

		for(Map<String,Object> orderdetai: orderdetailPartsInfoList){
			if((int)orderdetai.get("appraise_manner")+(int)orderdetai.get("appraise_result")+(int) orderdetai.get("appraise_productivity")<9){
				Map<String,Object> map=new HashMap<>();
				map.put("id",orderdetai.get("id"));
				map.put("create_time",orderdetai.get("appraise_time"));
				map.put("appraise_manner",orderdetai.get("appraise_manner"));
				map.put("appraise_result",orderdetai.get("appraise_result"));
				map.put("appraise_productivity",orderdetai.get("appraise_productivity"));
				resultList.add(map);
			}
		}
//		resultList = orderdetailPartsInfoList.parallelStream().filter(item ->{
//			long startTime = DateUtil.date2TimeStamp(date2.toString(), "yyyy-MM-dd");
//			long time = DateUtil.date2TimeStamp(item.get("appraise_time").toString().substring(0,10), "yyyy-MM-dd");
//			Integer appraise_manner =Integer.valueOf(Optional.ofNullable(item.get("appraise_manner")).orElse("0").toString());
//			Integer appraise_result =Integer.valueOf(Optional.ofNullable(item.get("appraise_result")).orElse("0").toString());
//			Integer appraise_productivity =Integer.valueOf(Optional.ofNullable(item.get("appraise_productivity")).orElse("0").toString());
//			return time >= startTime && appraise_manner+appraise_result+appraise_productivity < 12;
//
//		}).collect(Collectors.toCollection(ArrayList :: new));

		return resultList;
	}
	@Override
	public List<Map<String, Object>> getDictionariesInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = null;
		//获取所有的工单明细配件详细信息
		List<Map<String, Object>> orderdetailPartsInfoList = kanBanDao.getDictionariesInfo(reqMap);
		//根据配件名称进行分组,然后再按配件型号在配件名称的基础上进行分组(即进行多级分组)
		Map<String, Map<String, List<Map<String, Object>>>> partsMap = orderdetailPartsInfoList.parallelStream().collect(Collectors.groupingBy(map -> {

			if (Optional.ofNullable(map.get("dictionaries_name")).isPresent()) {

				return map.get("dictionaries_name").toString();

			}else {

				return "我是管理员给的默认故障类型";

			}



		},Collectors.groupingBy(map -> {

			if (Optional.ofNullable(map.get("car_type")).isPresent()) {

				return map.get("car_type").toString();

			}else {

				return "我是管理员给的默认车型";

			}

		})));
		//统计各配件的结果数量
		resultList =partsMap.entrySet().parallelStream().map(item ->{
			//声明集合容器
			Map<String,Object> map = new HashMap<String,Object>();
			//获取同一名称的保养配件的总数量
			item.getValue().entrySet().stream().forEach(a ->{
				//统计对应配件对应名称对应型号的数量
				long quantity = a.getValue().stream().count();
				//封装结果集
				map.put("dictionaries_name", item.getKey());
				map.put("car_type", a.getKey());
				map.put("quantity", quantity);

			});

			return map;

		}).collect(Collectors.toCollection(ArrayList :: new));

		return resultList;
	}
	/**
	 * 客户信息统计
	 */
	@Override
	public List<Map<String, Object>> getContactInfo(Map<String, Object> reqMap) throws Exception {
		// 声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
		// 获取所有的车主车辆关联表信息
		List<Map<String, Object>> contactInfoList = kanBanDao.getContactInfo(reqMap);
		for (Map<String, Object> o_id :contactInfoList) {
			Map<String, Object> resultMap = new HashMap<>();
			//获取车主的所有工单
			List<Map<String, Object>> orderInfoList = null;
			try {
				orderInfoList = kanBanDao.getOrderInfoByCarOwerIdTo(o_id.get("car_ower_id").toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
			//车辆总数
			int carCount = kanBanDao.getCarCount(o_id.get("car_ower_id").toString());

			Map<String, Object> weiXiuOrder=weiXiOrBaoyangCount(orderInfoList);
			resultMap.put("car_count", carCount);
			resultMap.put("car_name", o_id.get("user_name"));
			resultMap.put("mobile_phone", o_id.get("mobile_phone"));
			resultMap.put("weixiuordercount", weiXiuOrder.get("yuWeiXi"));
			resultMap.put("baoyangordercount", weiXiuOrder.get("yuBaoYang"));
			//总金额
			BigDecimal zongCount = new BigDecimal("0");
			if(orderInfoList != null && orderInfoList.size() > 0) {
				for (Map<String, Object> shouRuMap : orderInfoList) {
					if((int)shouRuMap.get("payment_status")==2){
						zongCount =zongCount.add(new BigDecimal(shouRuMap.get("payment_amount").toString()));
					}
				}
			}
			// 将总金额加入结果集
			resultMap.put("sumpaymentamount", zongCount);

			resultList.add(resultMap);
		}


		return resultList;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getWorkHoursInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = null;
		//获取所有的车辆信息列表
		List<Map<String, Object>> carInfoList = kanBanDao.getCarInfoList(reqMap);
		//根据s_id查询服务商信息
		Map<String, Object> serverInfo = kanBanDao.getServerInfoBySId(reqMap);
		Map<String, Double> doubleMap = new HashMap<>();
		resultList = carInfoList.parallelStream().map(item ->{
			//调用物联网卡地址接口查询信息
			String thingsInfo = HttpClient.doGet(Constants.THINGS_URL_ONE + item.get("opc_path"),serverInfo.get("interface_account") + "", serverInfo.get("unify_account") + "");
			//获取该车辆注册后运行的工时
			List<Map<String,Object>> list =JSONObject.parseObject(thingsInfo, List.class);
			//新思联接口获取车辆注册后的运行工时待计算
			if (null!=list&&list.size() > 0) {
				list.forEach(dataTime -> {
					List<Map<String,Object>> valueList = JSONObject.parseObject(dataTime.get("OPCValueList").toString(),List.class);
					valueList.forEach(m ->{
						if ("运行小时".equals(m.get("ValueName").toString())) {
							doubleMap.put("runHours", Double.valueOf(m.get("DataValue").toString())+ Double.valueOf(item.get("running").toString()));
						}
					});
				});
			}else{
				doubleMap.put("runHours",Double.valueOf(item.get("running").toString()));
			}

			//将运行工时添加到结果集
			item.put("runningHours", doubleMap.get("runHours"));

			return item;

		}).collect(Collectors.toCollection(LinkedList :: new));

		return resultList;
	}
	@Override
	public List<Map<String, Object>> getWorkHoursInfoNew(Map<String, Object> reqMap){
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList;
		//获取所有的车辆信息列表
		List<Map<String, Object>> carInfoList = kanBanDao.getCarInfoList(reqMap);
		Map<String, Double> doubleMap = new HashMap<>();
		resultList = carInfoList.stream().map(item ->{

			String thingsId = item.get("things_id").toString();
			Map<String, Object> pcCarMap = toolCarDataMapper.getPcCar(thingsId);
			if (pcCarMap.get("device_template") == null) {
				 item.put("runningHours", doubleMap.get("runHours"));
				 return item;
			}
			String adviceTemplateId = pcCarMap.get("device_template").toString();
			String tableName = ThingsData.getTableName(adviceTemplateId);
			// 获取车辆最新设备信息
			Map<String, Object> carDataMap = toolCarDataMapper.lastCarData(tableName, thingsId);
			if (carDataMap != null) {
				doubleMap.put("runHours", Double.valueOf(carDataMap.get("OperatingHours").toString())+ Double.valueOf(item.get("running").toString()));
			} else{
				doubleMap.put("runHours",Double.valueOf(item.get("running").toString()));
			}
			//将运行工时添加到结果集
			item.put("runningHours", doubleMap.get("runHours"));
			return item;
		}).collect(Collectors.toCollection(LinkedList :: new));

		return resultList;
	}
	/**
	 * 客服管理
	 */
	@Override
	public List<Map<String, Object>> getKfManageInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<>();

		reqMap.put("orderStatus", 3);
		//获取所有的工单信息
		List<Map<String, Object>> orderInfoList = getDayOrderList(reqMap);

		//将工单按客服进行分组
		List<Map<String, Object>> kfMap = orderInfoList.stream().collect(Collectors.groupingBy(map -> map.get("u_s_id").toString()))
				.entrySet()
				.stream()
				.map(e -> {
					Map<String, Object> map = new HashMap<>();
					map.put(e.getKey().toString(), e.getValue());
					return map;
				}).collect(Collectors.toList());

		for (Map<String, Object> map:kfMap) {
			//声明一个用来存储每行记录的map
			Iterator it=map.entrySet().iterator();
//			List<Map<String, Object>> orderList = null;
//			while(it.hasNext()){
//				Map.Entry entry = (Map.Entry)it.next();
//				key=entry.getKey();
//				value=entry.getValue();
//			}
			Map<String, Object> resultMap = new HashMap<>();
			while(it.hasNext()){
				Map.Entry entry = (Map.Entry)it.next();
				reqMap.put("u_s_id",entry.getKey());
				//获取接单总数
				List<Map<String, Object>> orderList = (List<Map<String, Object>>) entry.getValue();
//				try {
//					orderList = kanBanDao.getCountOrderInfoList(reqMap);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
				resultMap.put("user_name", orderList.get(0).get("user_name"));
				long resultTime=0;
				for (Map<String, Object> orderMap:orderList) {
					//添加平均响应时间
					if(orderMap.containsKey("start_handle_time")&&orderMap.get("start_handle_time")!=null&&
					orderMap.containsKey("end_handle_time")&&orderMap.get("end_handle_time")!=null
					){
						long responseTime = DateUtil.date2TimeStamp(orderMap.get("end_handle_time").toString(), "yyyy-MM-ddHH:mm:ss") - DateUtil.date2TimeStamp(orderMap.get("start_handle_time").toString(), "yyyy-MM-ddHH:mm:ss");
						resultTime=resultTime+responseTime;
					}

//					String start_handle_time =orderMap.get("start_handle_time").toString();
//					String end_handle_time = orderMap.get("end_handle_time").toString();
//					if (!StringUtils.isEmpty(start_handle_time) && !StringUtils.isEmpty(end_handle_time)) {
//						//单个工单的平均时间
//						long responseTime = DateUtil.date2TimeStamp(end_handle_time, "yyyy-MM-ddHH:mm:ss") - DateUtil.date2TimeStamp(start_handle_time, "yyyy-MM-ddHH:mm:ss");
//						resultTime=resultTime+responseTime;
//					}

				}
				resultMap.put("responseTime",BigDecimal.valueOf((float)resultTime/(float)orderList.size()/3600).setScale(2, RoundingMode.DOWN));
				// 添加客服接单量
				resultMap.put("jiedancount", orderList.size());

			}
			resultList.add(resultMap);
		}
		return resultList;
	}
	/**
	 * 获取近30的工单
	 * @param reqMap
	 * @return
	 */
	List<Map<String, Object>> getDayOrderList(Map<String, Object> reqMap){
		//获取当天的日期
		LocalDate date = LocalDate.now();
		//默认近30天
		LocalDate date2 = date.minusDays(29);
		reqMap.put("LocalDate",java.sql.Date.valueOf(date2));
		//获取所有的工单信息
		return kanBanDao.getOrderAndStatusList(reqMap);
	}


	@Override
	public List<Map<String, Object>> getCgManageInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<>();

		reqMap.put("orderStatus", 6);
		//获取所有的工单信息
		List<Map<String, Object>> orderInfoList = getDayOrderList(reqMap);
//		//获取当天的日期
//		LocalDate date = LocalDate.now();
//		//默认近30天
//		LocalDate date2 = date.minusDays(30);
//		//获取近30天的所有工单
//		List<Map<String, Object>> orderInfoList1 = orderInfoList.parallelStream().filter(item ->{
//
//			long startTime = DateUtil.date2TimeStamp(date2.toString(), "yyyy-MM-dd");
//			long time = DateUtil.date2TimeStamp(item.get("create_time").toString().substring(0,10), "yyyy-MM-dd");
//			return time >= startTime;
//
//		}).collect(Collectors.toCollection(ArrayList :: new));
		//将工单按仓管进行分组
		Map<String, List<Map<String, Object>>> cgMap = orderInfoList.stream().collect(Collectors.groupingBy(map ->{

			if (Optional.ofNullable(map.get("user_name")).isPresent()) {

				return map.get("user_name").toString();

			}else {

				return "我是管理员给的默认仓管";

			}
		}));
		//获取仓管管理信息
		cgMap.entrySet().parallelStream().forEach(a ->{
			//声明一个用来存储每行记录的map
			Map<String, Object> resultMap = new HashMap<>();
			//添加仓管姓名
			resultMap.put("user_name", a.getKey());
			//添加仓管接单量
			resultMap.put("dataList",a.getValue());
			//添加平均响应时间
			Double responseTime = a.getValue().stream().mapToDouble(k ->{
				String start_handle_time="0.0";
				String end_handle_time="0.0";
				if(k.containsKey("start_handle_time")&&null!=k.get("start_handle_time")){
					start_handle_time = k.get("start_handle_time").toString();
				}
				if(k.containsKey("end_handle_time")&&null!=k.get("end_handle_time")){
					end_handle_time =k.get("end_handle_time").toString();
				}
//				String start_handle_time = kanBanDao.findStartHandTimeByIdAndOrderstatus(k.get("id").toString(),Constants.ORDER_STATUS_PJ);
//				String end_handle_time = kanBanDao.findStartHandTimeByIdAndOrderstatus(k.get("id").toString(),Constants.ORDER_STATUS_DLL);
				//如果查出为null，则给默认值0.0
				String end = Optional.ofNullable(end_handle_time).orElseGet(() ->{
					return  "0.0";
				});
				//如果查出为null，则给默认值0.0
				String start = Optional.ofNullable(start_handle_time).orElseGet(() ->{
					return  "0.0";
				});
				if (end.equals("0.0") || start.equals("0.0")) {
					return 0.0;
				}
				long resultTime = DateUtil.date2TimeStamp(end, "yyyy-MM-ddHH:mm:ss") - DateUtil.date2TimeStamp(start, "yyyy-MM-ddHH:mm:ss");
				return resultTime;
			}).average().orElseGet(() ->{
				//如果不存在则给0.0
				return  0.0;

			});
			resultMap.put("responseTime",BigDecimal.valueOf(responseTime/3600).setScale(2, RoundingMode.DOWN));
			// 添加仓管接单量
			resultMap.put("jiedancount", a.getValue().toArray().length);

			resultList.add(resultMap);
		});
		return resultList;
	}
	@Override
	public List<Map<String, Object>> getTzManageInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<>();

		//获取所有听诊的工单信息
		reqMap.put("r_id","2");
		List<Map<String, Object>> orderInfoList = kanBanDao.getOrderInfoList1(reqMap);
		//获取当天的日期
		LocalDate date = LocalDate.now();
		//默认近30天
		LocalDate date2 = date.minusDays(30);
		//获取近30天的所有工单
		List<Map<String, Object>> orderInfoList1 = orderInfoList.parallelStream().filter(item ->{

			long startTime = DateUtil.date2TimeStamp(date2.toString(), "yyyy-MM-dd");
			long time = DateUtil.date2TimeStamp(item.get("create_time").toString().substring(0,10), "yyyy-MM-dd");
			return time >= startTime;

		}).collect(Collectors.toCollection(ArrayList :: new));
		//将工单按听诊进行分组
		Map<String, List<Map<String, Object>>> tzMap = orderInfoList1.stream().collect(Collectors.groupingBy(map ->{

			if (Optional.ofNullable(map.get("user_name")).isPresent()) {

				return map.get("user_name").toString();

			}else {

				return "我是管理员给的默认听诊";

			}
		}));
		// 获取听诊管理信息
		tzMap.entrySet().parallelStream().forEach(a ->{
			//声明一个用来存储每行记录的map
			Map<String, Object> resultMap = new HashMap<>();
			//添加听诊姓名
			resultMap.put("user_name", a.getKey());
			//添加听诊接单量
			resultMap.put("jiedancount", a.getValue().size());
			//添加平均响应时间
			Double responseTime = a.getValue().stream().mapToDouble(k ->{
				// 听诊管理修复
				String start_handle_time = kanBanDao.findStartHandTimeByIdAndOrderstatus(k.get("id").toString(),Constants.ORDER_STATUS_DTZ);
				String end_handle_time = kanBanDao.findStartHandTimeByIdAndOrderstatus(k.get("id").toString(),Constants.ORDER_STATUS_PJ);
				//如果查出为null，则给默认值0.0
				String end = Optional.ofNullable(end_handle_time).orElseGet(() ->{
					return  "0.0";
				});
				//如果查出为null，则给默认值0.0
				String start = Optional.ofNullable(start_handle_time).orElseGet(() ->{
					return  "0.0";
				});
				if (end.equals("0.0") || start.equals("0.0")) {
					return 0.0;
				}
				long resultTime = DateUtil.date2TimeStamp(end, "yyyy-MM-ddHH:mm:ss") - DateUtil.date2TimeStamp(start, "yyyy-MM-ddHH:mm:ss");
				return resultTime;
			}).average().orElseGet(() ->{
				//如果不存在则给0.0
				return  0.0;

			});
			resultMap.put("responseTime",BigDecimal.valueOf(responseTime/3600).setScale(2, RoundingMode.DOWN));
			resultList.add(resultMap);
		});
		return resultList;
	}

	@Override
	public List<Map<String, Object>> getXgManageInfo(Map<String, Object> reqMap) throws Exception {
		//声明一个用来存储返回结果的List
		List<Map<String, Object>> resultList = new LinkedList<>();
		//获取所有的工单信息
		List<Map<String, Object>> orderInfoList = kanBanDao.getOrderAndRepairInfoList(reqMap);

		//获取当天的日期
		LocalDate date = LocalDate.now();
		//默认近30天
		LocalDate date2 = date.minusDays(30);
		//获取近30天的所有工单
		List<Map<String, Object>> orderInfoList1 = orderInfoList.parallelStream().filter(item ->{

			long startTime = DateUtil.date2TimeStamp(date2.toString(), "yyyy-MM-dd");
			long time = DateUtil.date2TimeStamp(item.get("create_time").toString().substring(0,10), "yyyy-MM-dd");
			return time >= startTime;

		}).collect(Collectors.toCollection(ArrayList :: new));

		//将工单按修工进行分组
		Map<String, List<Map<String, Object>>> xgMap = orderInfoList1.stream().collect(Collectors.groupingBy(map ->{

			if (Optional.ofNullable(map.get("user_name")).isPresent()) {

				return map.get("user_name").toString();

			}else {

				return "我是管理员给的默认修工";

			}
		}));
		//获取修工管理信息
		xgMap.entrySet().parallelStream().forEach(a ->{
			//声明一个用来存储每行记录的map
			Map<String, Object> resultMap = new HashMap<>();
			//添加修工名字
			resultMap.put("user_name", a.getKey());
			//添加接单量
			resultMap.put("jiedancount", a.getValue().size());
			//将修工的id放入查询条件中
			reqMap.put("u_id", a.getValue().get(0).get("u_id"));
			reqMap.put("tsod_status",7);

			List<Map<String, Object>> xgOrderInfoList = kanBanDao.getOrderInfoListByXgIdAndStatus(reqMap);

			//好评的单子数量
			Long haopingCount = 0L;
			if(xgOrderInfoList!=null &&  xgOrderInfoList.size() != 0){
				//好评的单子数量
				haopingCount = xgOrderInfoList.stream().filter(map -> {
					// 这里异常报错
					Double appraiseManner = (Double) map.get("appraise_manner");
					int appraise_manner = appraiseManner != null ? appraiseManner.intValue() : 0;
					Double appraiseResult = (Double) map.get("appraise_result");
					int appraise_result = appraiseManner != null ? appraiseResult.intValue() : 0;
					Double appraiseProductivity = (Double) map.get("appraise_productivity");
					int appraise_productivity = appraiseManner != null ? appraiseProductivity.intValue() : 0;

//					Integer appraise_manner =Integer.valueOf(Optional.ofNullable(map.get("appraise_manner")).orElse("0").toString());
//					Integer appraise_result =Integer.valueOf(Optional.ofNullable(map.get("appraise_result")).orElse("0").toString());
//					Integer appraise_productivity =Integer.valueOf(Optional.ofNullable(map.get("appraise_productivity")).orElse("0").toString());
					return appraise_manner+appraise_result+appraise_productivity >= 9;

				}).count();
			}

			//添加好评的单子
			resultMap.put("happingcount", haopingCount);

			//则每个工单所花费的时间如下(单位为小时)
//			BigDecimal.valueOf(zoptionalList.size() == 0 ? 0.0 :(float)haopingCount/(float)zoptionalList.size()).setScale(2, RoundingMode.DOWN)
			//将平均维修时间加入结果集
			if(xgOrderInfoList==null || xgOrderInfoList.size()==0){
				resultMap.put("responseTime",0);
			}else{
				//遍历每一个工单,求得所有工单花费的总时间(单位为秒)
				Long totalTimeSecond = xgOrderInfoList.stream().mapToLong(item -> {

					Long handle_time = DateUtil.date2TimeStamp(item.get("handle_time").toString(), "yyyy-MM-ddHH:mm:ss");
					Long order_creat_time = DateUtil.date2TimeStamp(item.get("order_creat_time").toString(), "yyyy-MM-ddHH:mm:ss");
					return handle_time-order_creat_time;

				}).reduce(0,(x,y) -> x+y);

				resultMap.put("responseTime", BigDecimal.valueOf((float)totalTimeSecond /3600).setScale(2, RoundingMode.DOWN));
			}

//			System.out.println(BigDecimal.valueOf((float)totalTimeSecond /(float) xgOrderInfoList.size() /3600).setScale(2, RoundingMode.DOWN));

			// 平均修理时间
			// 这里先通过用户id，订单号，roleType=3，查看check表，computing_time 字段当作修理时间返回，虽然计算的是错误的，貌似少算12个小时
//			String userId = Orgin.getString(params, "userId");
			Double averageRepairTime = 0.0;
			Double repairTime = 0.0;
			for (Map<String, Object> item:xgOrderInfoList) {

				List<Map<String, Object>> maps = orderDao.selectByOrderNoAndUserId(item.get("u_s_id").toString(), item.get("order_no").toString());
				if(maps.size() != 0){
					for (Map<String, Object> item2 : maps) {
						repairTime += Double.parseDouble(item2.get("computing_time").toString());
					}
					averageRepairTime = repairTime/maps.size();
				}
			}

			resultMap.put("averageTime", BigDecimal.valueOf(averageRepairTime/3600).setScale(2,BigDecimal.ROUND_DOWN));
			//添加平均响应时间 这里的逻辑是错误的，那个detail表中的数据不是匹配的
//			reqMap.put("tsod_status",13);
//			List<Map<String, Object>> xgOrder = kanBanDao.getOrderInfoListByXgIdAndStatus(reqMap);
//			double responseTime=xgOrderInfoList.stream().mapToDouble(k ->{
//				String start_handle_time=k.get("handle_time").toString();
//				String end_handle_time="";
//				for(Map xg:xgOrder){
//					if(xg.get("id").toString().equals(k.get("id"))){
//						end_handle_time=xg.get("handle_time").toString();
//					}
//				}
//				//如果查出为null，则给默认值0.0
//				String end = Optional.ofNullable(end_handle_time).orElseGet(() ->{
//					return  "0.0";
//				});
//				//如果查出为null，则给默认值0.0
//				String start = Optional.ofNullable(start_handle_time).orElseGet(() ->{
//					return  "0.0";
//				});
//				if (end.equals("0.0") || start.equals("0.0")) {
//					return 0.0;
//				}
//				long resultTime = DateUtil.date2TimeStamp(end, "yyyy-MM-ddHH:mm:ss") - DateUtil.date2TimeStamp(start, "yyyy-MM-ddHH:mm:ss");
//				return resultTime;
//			}).average().orElseGet(() ->{
//				//如果不存在则给0.0
//				return  0.0;
//			});
//
//			if(responseTime == 0.0){
//				resultMap.put("averageTime","0.0");
//			}else{
//				// 怎么算出的是负值
//				resultMap.put("averageTime",BigDecimal.valueOf((float)responseTime / (float) xgOrderInfoList.size()/3600).setScale(2, RoundingMode.DOWN));
//			}

			resultList.add(resultMap);

		});

		return resultList;

	}

	@Override
	public List<Map<String, Object>> getXgListInfo(Map<String, Object> reqMap) {
		return kanBanDao.getXgListInfo(reqMap);
	}

	@Override
	public Map<String, Object> workOrderAmount(Map<String, Object> reqMap) {
		Map<String, Object> pageMap = Page.appPage(reqMap.get("pageSize").toString(),
				reqMap.get("currentPage").toString());
		reqMap.put("pageSize", pageMap.get("limit"));
		reqMap.put("currentPage", pageMap.get("offset"));
		if(null==reqMap.get("start_time")){
			//获取当前月第一天：
			LocalDate date=LocalDate.now();
			LocalDate firstDayOfThisMonth = date.with(TemporalAdjusters.firstDayOfMonth());
			reqMap.put("start_time", Timestamp.valueOf( firstDayOfThisMonth.toString()+" 00:00:00"));

			LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
			reqMap.put("end_time",Timestamp.valueOf(lastDayOfThisMonth.toString()+" 23:59:59"));
		}else{
			reqMap.put("start_time",Timestamp.valueOf(reqMap.get("start_time")+"-01"+" 00:00:00"));

			if(null!=reqMap.get("end_time")){
				LocalDate date=LocalDate.parse(reqMap.get("end_time").toString()+"-01");
				LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
				reqMap.put("end_time",Timestamp.valueOf(lastDayOfThisMonth.toString()+" 23:59:59"));
			}else{
				LocalDate date=LocalDate.now();
				LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
				reqMap.put("end_time",Timestamp.valueOf(lastDayOfThisMonth.toString()+" 23:59:59"));
			}

		}

		List<Map<String, Object>> mapList=kanBanDao.workOrderAmount(reqMap);
		for(Map<String, Object> map:mapList){
			if(null!=map.get("payment_amount")&&(Float.parseFloat(map.get("payment_amount").toString()))>0) {//总价
				if(null!=map.get("xamount")&&Float.parseFloat(map.get("xamount").toString())>0){
//					pjTotalSum+=Float.parseFloat(map.get("payment_amount").toString())-Float.parseFloat(map.get("xamount").toString());
//					wxTotalSum+=wxTotalSum+Float.parseFloat(map.get("xamount").toString());
					map.put("pjamount",Float.parseFloat(map.get("payment_amount").toString())-Float.parseFloat(map.get("xamount").toString()));
				}else{
//					pjTotalSum+=Float.parseFloat(map.get("payment_amount").toString());
					map.put("pjamount",map.get("payment_amount"));
					map.put("xamount",0);
				}
			}
		}
		Map<String,Object> map=kanBanDao.workOrderCount(reqMap);
		Map<String, Object> result=new HashMap<>();
		result.put("workOrderAmountList",mapList);
		if(null!=map.get("xamount")&&!map.get("xamount").toString().equals("")){
			result.put("wxTotalSum",map.get("xamount"));
		}else{
			result.put("wxTotalSum",0);
		}
		if(null!=map.get("totalsum")&&!map.get("totalsum").equals("")){
			result.put("pjTotalSum",Float.parseFloat(map.get("totalsum").toString())-Float.parseFloat(result.get("wxTotalSum").toString()));
		}else{
			result.put("pjTotalSum",0);
		}
		result.put("count",map.get("count"));
		return result;
	}

	/**
	 *  修工金额统计
	 */
	@Override
	public Map<String, Object> repairAmount(Map<String, Object> reqMap) {
		Map<String, Object> pageMap = Page.appPage(reqMap.get("pageSize").toString(),
				reqMap.get("currentPage").toString());
		reqMap.put("pageSize", pageMap.get("limit"));
		reqMap.put("currentPage", pageMap.get("offset"));
		if(null==reqMap.get("start_time")){
			//获取当前月第一天：
			LocalDate date=LocalDate.now();
			LocalDate firstDayOfThisMonth = date.with(TemporalAdjusters.firstDayOfMonth());
			reqMap.put("start_time", Timestamp.valueOf( firstDayOfThisMonth.toString()+" 00:00:00"));
			LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
			reqMap.put("end_time",Timestamp.valueOf(lastDayOfThisMonth.toString()+" 23:59:59"));
		}else{
			reqMap.put("start_time",Timestamp.valueOf(reqMap.get("start_time")+"-01"+" 00:00:00"));
			if(null!=reqMap.get("end_time")){
				LocalDate date=LocalDate.parse(reqMap.get("end_time").toString()+"-01");
				LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
				reqMap.put("end_time",Timestamp.valueOf(lastDayOfThisMonth.toString()+" 23:59:59"));
			}else{
				LocalDate date=LocalDate.now();
				LocalDate lastDayOfThisMonth = date.with(TemporalAdjusters.lastDayOfMonth());
				reqMap.put("end_time",Timestamp.valueOf(lastDayOfThisMonth.toString()+" 23:59:59"));
			}
		}

		Map<String, Object> result=new HashMap<>();
		List<Map<String, Object>> mapList =kanBanDao.repairAmount(reqMap);
		for(Map<String, Object> map:mapList){
			if(null!=map.get("amount")&&Float.parseFloat(map.get("amount").toString())>0) {//总价
				if(null!=map.get("xamount")&&Float.parseFloat(map.get("xamount").toString())>0){
//					pjTotalSum+=Float.parseFloat(map.get("payment_amount").toString())-Float.parseFloat(map.get("xamount").toString());
//					wxTotalSum+=wxTotalSum+Float.parseFloat(map.get("xamount").toString());
					map.put("pjamount",Float.parseFloat(map.get("amount").toString())-Float.parseFloat(map.get("xamount").toString()));
				}else{
					map.put("pjamount",map.get("amount"));
					map.put("xamount",0);
				}
			}
		}
		result.put("count",kanBanDao.repairCount(reqMap).size());
		result.put("repairAmountList",mapList);
//		result.put("wxTotalSum",wxTotalSum);
//		result.put("pjTotalSum",pjTotalSum);
//		result.put("count",kanBanDao.repairCount(reqMap));
		return result;
	}


	/**
	 * 获取保养维修总数
	 * @param mapList
	 * @return
	 */
	private Map<String, Object> weiXiOrBaoyangCount(List<Map<String, Object>> mapList){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//维修
		int yuWeiXi = 0;
		//保养
		int yuBaoYang = 0;
		// 循环获取维修保养返修数据
		if(mapList != null && mapList.size() > 0 ) {
			for (Map<String, Object> yuyueMap : mapList) {
				//维修
				if (StringUtils.equals(yuyueMap.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_WX))) {
					yuWeiXi =yuWeiXi+1;
				}
				//保养
				if(StringUtils.equals(yuyueMap.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_BY))) {
					yuBaoYang =yuBaoYang+1;
				}
				//维修加保养算维修
				if(StringUtils.equals(yuyueMap.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_WXBY))) {
					yuWeiXi =yuWeiXi+1;
				}
				//返修
				if(StringUtils.equals(yuyueMap.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_FX))) {
					//获取父级工单
					Map<String, Object> orderInfo = kanBanDao.getOrderInfo(yuyueMap.get("parent_id").toString());
					//维修
					if (StringUtils.equals(orderInfo.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_WX))) {
						yuWeiXi =yuWeiXi+1;
					}
					//保养
					if(StringUtils.equals(orderInfo.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_BY))) {
						yuBaoYang =yuBaoYang+1;
					}
					//维修加保养算维修
					if(StringUtils.equals(orderInfo.get("order_type").toString(), String.valueOf(Constants.ORDER_TYEP_WXBY))) {
						yuWeiXi =yuWeiXi+1;
					}
				}
			}
		}
		resultMap.put("yuWeiXi", yuWeiXi);
		resultMap.put("yuBaoYang", yuBaoYang);
		return resultMap;
	}

}
